// +----------------------------------------------------------------------
// | ILDP-three-dimensional系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.lyltop.top All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 彦麟工作室 <yanlinstudio@126.com>
// +----------------------------------------------------------------------
// | Name: 刘彦麟
// +----------------------------------------------------------------------
// | FILE_NAME: rendererMousedown.ts
// +----------------------------------------------------------------------
// | Date: 2022/6/14 09:54
// +----------------------------------------------------------------------

import * as THREE from "three";
import {Camera, Raycaster, Scene, WebGLRenderer} from "three";
import {Object3D} from "three/src/Three";

/**
 * 鼠标按下功能
 *
 * */
class RendererMousedown {
    private mouse = {x: 0, y: 0};
    private raycaster: Raycaster = new THREE.Raycaster();
    private renderer: WebGLRenderer;
    private scene: Scene;
    private camera: Camera;
    private ObjectName: Array<string> = ["XYZ", "gridHelperSc", "translate", "Line", "X", "Y", "Z", "transformControl"]

    constructor(renderer: WebGLRenderer, scene: Scene, camera: Camera) {
        this.renderer = renderer;
        this.scene = scene;
        this.camera = camera;
    }

    public OnTouchMove(events: any, width: number, height: number, object3DList: Object3D[]): Object3D | null {
        let x, y;
        if (events.changedTouches) {
            x = events.changedTouches[0].pageX;
            y = events.changedTouches[0].pageY;
        } else {
            x = events.clientX;
            y = events.clientY;
        }
        this.mouse.x = ((x - 200) / width) * 2 - 1;
        this.mouse.y = -(y / height) * 2 + 1;
        return this.CheckIntersection(object3DList);
    }

    private CheckIntersection(object3DList: Object3D[]): Object3D | null {
        this.raycaster.setFromCamera(this.mouse, this.camera);
        let selectedObject: Object3D | null = null;
        const intersects = this.raycaster.intersectObjects(object3DList);
        if (intersects.length > 0) {
            selectedObject = intersects[0].object;
        } else {
        }
        return selectedObject;
    }
}

export default RendererMousedown
